package fabricas;

import java.util.HashMap;

/**
 * Trabalho DAS - 2014/2015
 *
 * @author a21210380, a21190325, a21210392
 */
public class FabricaPool {
    private final static HashMap<Class<?>, Pool> typePools = new HashMap<>();

    public static <T> void createPool(Class<T> poolClass, Pool<T> pool) {
        if (!typePools.containsKey(poolClass)) {
            typePools.put(poolClass, pool);
        }
    }
    
    public static <T> T obtain(Class<T> poolClass) {
        if (typePools.containsKey(poolClass)) {
            return (T) typePools.get(poolClass).obter();
        }
        return null;
    }
    
    public static <T> void free(T freeObject) {
        Class<T> objClass = (Class<T>) freeObject.getClass();
        if (typePools.containsKey(objClass)) {
            typePools.get(objClass).libertar(freeObject);
        }
    }

    private FabricaPool() {}
}
